package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.atguigu.guli.service.edu.service.VideoService;
import com.atguigu.guli.service.result.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/edu/chapter")

public class AdminChapterController {

    @Autowired
    ChapterService chapterService;

    @PostMapping("/add") //添加章节
    public R add(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    @Autowired
    VideoService videoService;
    @DeleteMapping("/del/{id}") //删除章节
    public R del(@PathVariable String id){
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = videoService.count(wrapper);
        if (count>0){
            return R.error().message("不能删除");
        }
        chapterService.removeById(id);
        return R.ok();
    }

    @GetMapping("/getById/{id}") //根据id查询章节
    public R getById(@PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("list",chapter);
    }

    @PutMapping("/edit") //修改章节
    public R edit(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }

    @GetMapping("/getChapters/{crouseId}") //根据id查询章节
    public R getChapters(@PathVariable String crouseId){

        List<Chapter> chapters = chapterService.getChapterAndVideo(crouseId);

        return R.ok().data("lists",chapters);
    }

}

